package com.example.com.baruffini_mele.booking4u;

import java.util.ArrayList;
import java.util.Iterator;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class SelectRoomActivity extends Activity {
	private static final String TAG = "SelectRoomActivity";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try{
        setContentView(R.layout.activity_select_room_layout);
        Log.v(TAG, "carico layout");
        LinearLayout layout = (LinearLayout)findViewById(R.id.linear_select_room);
        LinearLayout.LayoutParams parametri = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );

        
        Log.v(TAG, "costruisco array list");
        GCalendar myCG = new GCalendar();
        ArrayList<GCalendar> currentCalendars = new ArrayList();
        // request current calendars to remote database
        currentCalendars = myCG.postCalendarsRequest();
      
       
        
        //dynamic button list generator
        
        Iterator<GCalendar> itr = currentCalendars.iterator();
        int i = 0;
        while (itr.hasNext()) {
        	GCalendar element = itr.next();
        	Log.v(TAG, "bottone "+i);
        	i++;
        	// button i
        	Button btn =  new Button(this);
            btn.setId(i);       
            btn.setText(element.getName());
            layout.addView(btn, parametri);
            final String calendarId = element.getCalendarId();
            final String calendarName = element.getName();
            btn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Perform action on click - start activity with parameter
                    Intent intent = new Intent(getBaseContext(), ShowRoomCalendarActivity.class);
                	intent.putExtra("calendarId",calendarId);
                	intent.putExtra("calendarName",calendarName);
                	startActivity(intent);
                	
                	
                }
            });
  
          }
        
        
        }catch(Exception e){
        	e.printStackTrace();
        }
       
        
    }
        
   
       
        
   

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_select_room_menu, menu);
        return true;
    }
    
    
    
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) 
        {
        	// if help selected
            case R.id.select_room_menu_home:
            	// start Main
               startActivity(new Intent(this, MainActivity.class));
            return true;
            
            
        }// end switch on menu item
        return(super.onOptionsItemSelected(item));
    }
}
